package com.uuah.server.config.spring.parsers.generic;

import org.w3c.dom.Element;

import com.uuah.server.config.spring.parsers.AbstractUuahBeanDefinitionParser;

/**
 * Contructs a single, standalone bean from an element - it is not injected into
 * any other object. This parser can be configured to automatically set the
 * class of the object, the init and destroy methods and whether this object is
 * a singleton.
 *
 * <p>
 * Typically, you should use {@link UuahOrphanDefinitionParser} instead of this
 * class, since these elements occur in the <uuah> top level element.
 * </p>
 */
public class OrphanDefinitionParser extends AbstractUuahBeanDefinitionParser {

	private Class beanClass = null;
	private boolean dynamicClass = false;

	/**
	 * This constructor assumes that the class name will be explicitly specified
	 * as an attribute on the element.
	 */
	public OrphanDefinitionParser(boolean singleton) {
		this.singleton = singleton;
		dynamicClass = true;
	}

	public OrphanDefinitionParser(Class beanClass, boolean singleton) {
		this.beanClass = beanClass;
		this.singleton = singleton;
	}

	// @Override
	protected void preProcess(Element element) {
		super.preProcess(element);
		// top level beans need an ID element
		AutoIdUtils.ensureUniqueId(element, "bean");
		if (dynamicClass) {
			beanClass = null;
		}
	}

	// @Override
	protected Class getBeanClass(Element element) {
		return beanClass;
	}

}